package com.cqan.message.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.cqan.account.entity.User;
import com.cqan.dao.hibernate.HibernateDao;
import com.cqan.message.entity.Message;
import com.cqan.orm.Page;

@Repository
public class MessageDao extends HibernateDao<Message,Long> {

	@SuppressWarnings("unchecked")
	public Page<Message> getInboxMessage(Page<Message> page, User user) {
		Criteria c = getSession().createCriteria(Message.class).createAlias("toUsers", "u").add(Restrictions.eq("u.id", user.getId()));
		if (page.isAutoCount()) {
			int totalCount = countCriteriaResult(c);
			page.setTotalCount(totalCount);
		}

		setPageParameterToCriteria(c, page);
		List<Message> result = c.list();
		page.setResult(result);
		return page;
	}

}
